package com.ship.dispatch.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.github.pagehelper.PageInfo;
import com.ship.common.core.utils.StringUtils;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.dispatch.bean.Compatibility.QueryShipAffiliatedDetailsRq;
import com.ship.dispatch.bean.Compatibility.QueryShipAffiliatedPortCollectRq;
import com.ship.dispatch.bean.Compatibility.QueryShipAffiliatedToPortRq;
import com.ship.dispatch.bean.Compatibility.QueryShipToPortRq;
import com.ship.dispatch.bean.Compatibility.vo.*;
import com.ship.dispatch.bean.SpBoastPortMessage;
import com.ship.dispatch.bean.boats.QueryBoatsPortHistoryRq;
import com.ship.dispatch.bean.boats.QueryPortSisterRq;
import com.ship.dispatch.bean.boats.ShipDashboarDataRq;
import com.ship.dispatch.bean.boats.info.QueryBoatsPortInfo;
import com.ship.dispatch.bean.boats.info.QueryBoatsPortSisterInfo;
import com.ship.dispatch.bean.boats.info.QueryPortInfo;
import com.ship.dispatch.service.BoatsVoyageMessageService;
import com.ship.dispatch.service.ExportService;
import com.ship.dispatch.service.ShipDashboarDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b>ShipVoyageCompatibilityController</b>
 *
 * @description: ShipVoyageCompatibilityController <br>
 * @date: 2024/6/20 15:26 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@RestController
@Api(tags = "船岸兼容-航次列表")
public class ShipVoyageCompatibilityController {
    private static final Logger logger = LoggerFactory.getLogger(ShipVoyageCompatibilityController.class);

    @Autowired
    private BoatsVoyageMessageService boatsVoyageMessageService;

    @Autowired
    private ExportService exportService;

    @Autowired
    private ShipDashboarDataService shipDashboarDataService;

    @ApiOperation("根据类型查询查询条件内容-港口挂靠船舶")
    @GetMapping("/query/shipMessage/byType")
    public JsonResult queryShipMessageByType(@RequestParam String type){
        List<String> messageList = boatsVoyageMessageService.queryMessage(type);
        return JsonResult.success(messageList);
    }

    @ApiOperation("分页查询船舶挂靠港口列表-船舶挂靠港口")
    @PostMapping("/query/shipAffiliatedPort/page")
    public JsonResult queryShipAffiliatedPortPage(@RequestBody QueryShipToPortRq rq){
        PageInfo<QueryShipToPortInfo> pageInfo = boatsVoyageMessageService.queryShipAffiliatedPortPage(rq);
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("导出船舶挂靠港口列表-船舶挂靠港口")
    @PostMapping("/export/shipAffiliatedPort")
    public JsonResult exportShipAffiliatedPort(@RequestBody QueryShipToPortRq rq) throws IOException, IllegalAccessException {
        List<QueryShipToPortInfo> shipToPortList = boatsVoyageMessageService.exportShipAffiliatedPort(rq);
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("shipName");
        tableNameList.add("imo");
        tableNameList.add("receivingStation");
        tableNameList.add("portOfCallNumber");
        List<String> tableList = new ArrayList<>();
        tableList.add("船舶名称");
        tableList.add("IMO");
        tableList.add("接收站名称");
        tableList.add("累计挂靠次数");
        Field[] fields = QueryShipToPortInfo.class.getDeclaredFields();
        String url = exportService.fileExport(shipToPortList, fields,
                tableNameList, tableList,
                "船舶挂靠港记录列表");
        return JsonResult.success(url);
    }

    @ApiOperation("根据imo号及接收站名称查询详情-船舶挂靠港口")
    @PostMapping("/query/shipAffiliatedPortDetails")
    public JsonResult queryShipAffiliatedPortDetails(@RequestBody QueryShipAffiliatedDetailsRq rq){
        QueryShipAffiliatedDetailsInfo info = boatsVoyageMessageService.queryShipAffiliatedPortDetails(rq);
        return JsonResult.success(info);
    }

    @ApiOperation("根据imo分页查询船舶挂靠港汇总列表-船舶挂靠港口")
    @PostMapping("/query/shipAffiliatedPort/collect")
    public JsonResult queryShipAffiliatedPortCollect(@RequestBody QueryShipAffiliatedPortCollectRq rq){
        PageInfo<QueryShipAffiliatedPortCollectInfo> pageInfo = boatsVoyageMessageService.queryShipAffiliatedPortCollect(rq);
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("导出船舶挂靠港口汇总列表-船舶挂靠港口")
    @PostMapping("/export/shipAffiliatedPort/collect")
    public JsonResult exportShipAffiliatedPortCollect(@RequestBody QueryShipAffiliatedPortCollectRq rq){
        List<QueryShipAffiliatedPortCollectInfo> portCollectList = boatsVoyageMessageService.exportShipAffiliatedPortCollect(rq);
        String shipName = boatsVoyageMessageService.queryShipNameByImo(rq.getImo());
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("berthName");
        tableNameList.add("affiliationsNumber");
        List<String> tableList = new ArrayList<>();
        tableList.add("泊位名称");
        tableList.add("挂靠次数");
        Field[] fields = QueryShipAffiliatedPortCollectInfo.class.getDeclaredFields();
        String url = exportService.exportUtil(portCollectList, fields,
                tableNameList, tableList,
                shipName + "挂靠港记录");
        return JsonResult.success(url);
    }

    @ApiOperation("根据imo号查询姊妹船列表-船舶挂靠港口")
    @PostMapping("/query/shipSister/page")
    public JsonResult queryBoatsPortSisterList(@RequestBody QueryPortSisterRq rq) {
        logger.info("根据imo号分页查询船舶挂靠港详情姊妹船列表入参:{}" + JSONUtil.parse(rq));
        List<QueryBoatsPortSisterInfo> boatsPortSisterList = boatsVoyageMessageService.queryBoatsPortSisterList(rq);
        logger.info("根据imo号分页查询船舶挂靠港详情姊妹船列表入参:{}" + JSONUtil.parse(boatsPortSisterList));
        return JsonResult.success(boatsPortSisterList);
    }

    @ApiOperation("导出姊妹船列表-船舶挂靠港口")
    @PostMapping("/export/shipSister")
    public JsonResult exportShipSister(@RequestBody QueryPortSisterRq rq){
        List<QueryBoatsPortSisterInfo> boatsPortSisterList = boatsVoyageMessageService.queryBoatsPortSisterList(rq);
        String shipName = boatsVoyageMessageService.queryShipNameByImo(rq.getImo());
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("shipName");
        tableNameList.add("sisterImo");
        List<String> tableList = new ArrayList<>();
        tableList.add("船舶名称");
        tableList.add("IMO");
        Field[] fields = QueryBoatsPortSisterInfo.class.getDeclaredFields();
        String url = exportService.exportUtil(boatsPortSisterList, fields,
                tableNameList, tableList,
                shipName + "姊妹船信息");
        return JsonResult.success(url);
    }

    @ApiOperation("分页查询船舶历史挂靠港记录-船舶挂靠港口")
    @PostMapping("/query/shipAffiliatedToPort/page")
    public JsonResult queryShipAffiliatedToPortPage(@RequestBody QueryShipAffiliatedToPortRq rq){
        PageInfo<QueryShipAffiliatedToPortPageInfo> pageInfo = boatsVoyageMessageService.queryShipAffiliatedToPortPage(rq);
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("导出船舶历史挂靠港记录-船舶挂靠港口")
    @PostMapping("/export/shipAffiliatedToPort")
    public JsonResult exportShipAffiliatedToPort(@RequestBody QueryShipAffiliatedToPortRq rq){
        List<QueryShipAffiliatedToPortPageInfo> list = boatsVoyageMessageService.exportShipAffiliatedToPort(rq);
        String shipName = boatsVoyageMessageService.queryShipNameByImo(rq.getImo());
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("portName");
        tableNameList.add("loadCountry");
        tableNameList.add("berthName");
        tableNameList.add("cargoProspectTotal");
        tableNameList.add("moorAlongsideTime");
        tableNameList.add("unberthTime");
        tableNameList.add("arrivalAtPortDraught");
        tableNameList.add("leavePortDraught");
        List<String> tableList = new ArrayList<>();
        tableList.add("接收站名称");
        tableList.add("装货国家");
        tableList.add("泊位名称");
        tableList.add("装/卸载量");
        tableList.add("靠泊时间");
        tableList.add("离泊时间");
        tableList.add("抵港吃水(米)");
        tableList.add("离港吃水(米)");
        Field[] fields = QueryShipAffiliatedToPortPageInfo.class.getDeclaredFields();
        String url = exportService.exportUtil(list, fields,
                tableNameList, tableList,
                shipName + "历史挂靠港记录");
        return JsonResult.success(url);
    }

    @ApiOperation("分页查询港口挂靠船舶列表-港口挂靠船舶")
    @PostMapping("/query/portAffiliatedShip/page")
    public JsonResult queryPortAffiliatedShipPage(@RequestBody QueryShipToPortRq rq){
        PageInfo<QueryShipToPortInfo> pageInfo = boatsVoyageMessageService.queryPortAffiliatedShipPage(rq);
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("导出港口挂靠船舶列表-港口挂靠船舶")
    @PostMapping("/export/portAffiliatedShip")
    public JsonResult exportPortAffiliatedShip(@RequestBody QueryShipToPortRq rq) throws IOException, IllegalAccessException {
        List<QueryShipToPortInfo> shipToPortList = boatsVoyageMessageService.exportShipAffiliatedPort(rq);
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("receivingStation");
        tableNameList.add("shipName");
        tableNameList.add("imo");
        tableNameList.add("portOfCallNumber");
        List<String> tableList = new ArrayList<>();
        tableList.add("接收站名称");
        tableList.add("船舶名称");
        tableList.add("IMO");
        tableList.add("累计挂靠次数");
        Field[] fields = QueryShipToPortInfo.class.getDeclaredFields();
        String url = exportService.fileExport(shipToPortList, fields,
                tableNameList, tableList,
                "港口挂靠船舶记录");
        return JsonResult.success(url);
    }

    @ApiOperation("根据imo号查询港口挂靠船舶详情-港口挂靠船舶")
    @PostMapping("/query/portAffiliatedShip/byImo")
    public JsonResult queryPortAffiliatedShipByImo(@RequestBody QueryShipAffiliatedDetailsRq rq){
        PortAffiliatedShipInfo info = boatsVoyageMessageService.queryPortAffiliatedShipByImo(rq);
        return JsonResult.success(info);
    }

    @ApiOperation("根据imo号分页查询港口挂靠船舶-港口挂靠船舶")
    @PostMapping("/query/portAffiliatedToShip")
    public JsonResult queryPortAffiliatedToShip(@RequestBody QueryShipAffiliatedToPortRq rq){
        PageInfo<QueryPortAffiliatedToShipInfo> pageInfo = boatsVoyageMessageService.queryPortAffiliatedToShip(rq);
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("根据imo号导出港口挂靠船舶-港口挂靠船舶")
    @PostMapping("/export/portAffiliatedToShip")
    public JsonResult exportPortAffiliatedToShip(@RequestBody QueryShipAffiliatedToPortRq rq) throws IOException, IllegalAccessException {
        List<QueryPortAffiliatedToShipInfo> affiliatedToShipList = boatsVoyageMessageService.exportPortAffiliatedToShip(rq);
        String shipName = boatsVoyageMessageService.queryShipNameByImo(rq.getImo());
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("shipName");
        tableNameList.add("imo");
        tableNameList.add("berthName");
        tableNameList.add("cargoProspectTotal");
        tableNameList.add("moorAlongsideTime");
        tableNameList.add("unberthTime");
        tableNameList.add("arrivalAtPortDraught");
        tableNameList.add("leavePortDraught");
        List<String> tableList = new ArrayList<>();
        tableList.add("船舶名称");
        tableList.add("IMO");
        tableList.add("泊位名称");
        tableList.add("装/卸载量");
        tableList.add("靠泊时间");
        tableList.add("离泊时间");
        tableList.add("抵港吃水(米)");
        tableList.add("离港吃水(米)");
        Field[] fields = QueryPortAffiliatedToShipInfo.class.getDeclaredFields();
        String url = exportService.exportUtil(affiliatedToShipList, fields,
                tableNameList, tableList,
                shipName + "历史挂靠港记录");
        return JsonResult.success(url);
    }

    @ApiOperation("测试推送关务系统数据")
    @PostMapping("/push/guanwu")
    public void pushGuanWuData(@RequestBody ShipDashboarDataRq rq) throws IOException {
        shipDashboarDataService.pushGuanWuData(rq);
    }
}
